package com.controller.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.DateUtils;
import com.dao.task.TaskMapper;
import com.model.sys.SysUser;

@Controller
public class DefaultController {

	static final Logger logger = LoggerFactory.getLogger(DefaultController.class);
	@Autowired
	private TaskMapper taskMapper;

	@RequestMapping("/task/default")
	public String taskDefault(HttpServletRequest request) {
		request.setAttribute("currentyear", DateUtils.getCurrentYear());
		request.setAttribute("currentweek", DateUtils.getWeek(new Date()));
		request.setAttribute("currentdate", DateUtils.getCurrentDate());
		return "task/TaskManage";
	}

	@RequestMapping("/task/statis")
	public String taskStatis(HttpServletRequest request) {
		request.setAttribute("currentyear", DateUtils.getCurrentYear());
		request.setAttribute("currentweek", DateUtils.getWeek(new Date()));
		request.setAttribute("currentdate", DateUtils.getCurrentDate());
		return "task/TaskStatis";
	}

	@RequestMapping(value = "/default/selectMytask", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String selectMytask(HttpServletRequest request) {
		try {
			HashMap<String, Object> hashtable = new HashMap<>();
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			int week = DateUtils.getWeek(new Date());
			String year = DateUtils.getCurrentYear();
			hashtable.put("week", year + week);
			hashtable.put("userid", sysUser.getUserid());
			hashtable.put("username", sysUser.getUsername());
			List<HashMap<String, Object>> list = taskMapper.selectMytask(hashtable);
			String jsonString = JSON.toJSONString(list);
			logger.info("selectMytask:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("selectMytask:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/default/selectWeektask", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String selectWeektask(HttpServletRequest request) {
		try {
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
			HashMap<String, Object> hashtable = new HashMap<>();
			int week = DateUtils.getWeek(new Date());
			String year = DateUtils.getCurrentYear();
			hashtable.put("week", year + week);
			hashtable.put("creatorid", sysUser.getUserid());
			hashtable.put("superadmin", sysUser.getSuperadmin());
//			hashtable.put("PAGENUM", 0);
//			hashtable.put("PAGESIZE", 10);
			List<HashMap<String, Object>> list = taskMapper.selectWeektask(hashtable);
			String jsonString = JSON.toJSONString(list);
			logger.info("selectWeektask:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("selectWeektask:{}", e.getMessage());
			return e.getMessage();
		}
	}
}
